#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "gestion_patient.h" //pour le volet métier de l'application
#include <stdlib.h> //pour l'appel de la fonction atoi()
#include <string.h> //pour l'appel de la fonction strcpy
#include "gestion_personnelnv.h"
#include "Authentification.h" 
#include <stdlib.h> //pour l'appel de la fonction atoi()
#include <string.h> //pour l'appel de la fonction strcpy
#include "parcours.h" //pour le volet métier de SUIVI_PERSONNEL
int test;
int nom1;
int prenom1;
int specialite1;
int choix;
char login [20];
char password [20];
FILE *f;
FILE *f1;
void
on_button1_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window2;
GtkWidget *window1;

window1=lookup_widget(objet_graphique, "window1");

gtk_widget_destroy(window1);
window2 = create_window2();
gtk_widget_show(window2);


}


void on_button49_clicked(GtkWidget*objet_graphique, gpointer user_data)
{GtkWidget *window3;
GtkWidget *window2;
choix=1;
window3=lookup_widget(objet_graphique, "window3");
gtk_widget_destroy(window2);
window3 = create_window3 ();
  gtk_widget_show (window3);

}


void
on_button54_clicked             (GtkWidget*objet_graphique, gpointer user_data)
{char buf1[20] ="";
GtkWidget *entry40;
GtkWidget *entry41;
GtkWidget *label84;
GtkWidget *window4;
GtkWidget *window3;
window4=lookup_widget(objet_graphique, "window4");
entry40=lookup_widget (objet_graphique, "entry40");
entry41=lookup_widget (objet_graphique, "entry41");
label84=lookup_widget (objet_graphique, "label84");
sprintf(buf1,"identifier vous svp %s",buf1);
gtk_label_set_text(GTK_LABEL(label84), buf1);
strcpy(login,gtk_entry_get_text(GTK_ENTRY(entry40)));
strcpy(password,gtk_entry_get_text(GTK_ENTRY(entry41)));

id_pass(login,password);
if(choix==1)
{f=fopen("code.txt","r");
if (res_aut( login,password,f)==1)
    {   
gtk_widget_destroy(window3);
window4 = create_window4 ();
  gtk_widget_show (window4);
}
  else {
sprintf(buf1,"Authentification erronne %s",buf1);
gtk_label_set_text(GTK_LABEL(label84), buf1);
}
}
if(choix==2)
{f1=fopen("code1.txt","r");
if (res_aut( login,password,f1)==1)
    {   
gtk_widget_destroy(window3);
window4 = create_window4 ();
  gtk_widget_show (window4);
}
else {
sprintf(buf1,"Authentification erronne %s ",buf1);
gtk_label_set_text(GTK_LABEL(label84), buf1);
}
}

}
void
on_button53_clicked (GtkWidget*objet_graphique, gpointer user_data)  
{GtkWidget *window2;
GtkWidget *window3;
gtk_widget_destroy(window3);
window2 = create_window2 ();
  gtk_widget_show (window2);
}


void
on_button52_clicked    (GtkWidget*objet_graphique, gpointer user_data)                
{char buf0[20] ="";
GtkWidget *entry40;
GtkWidget *entry41;
GtkWidget *label84;
GtkWidget *window24;
GtkWidget *window5;
GtkWidget *window3;
window5=lookup_widget(objet_graphique, "window5");
window24=lookup_widget(objet_graphique, "window24");
entry40=lookup_widget (objet_graphique, "entry40");
entry41=lookup_widget (objet_graphique, "entry41");
label84=lookup_widget (objet_graphique, "label84");

strcpy(login,gtk_entry_get_text(GTK_ENTRY(entry40)));
strcpy(password,gtk_entry_get_text(GTK_ENTRY(entry41)));

id_pass(login,password);
if(choix==1)
{f=fopen("code.txt","r");
if (res_aut( login,password,f)==1)
    {   
sprintf(buf0,"Authentification reussie %s",buf0);
gtk_label_set_text(GTK_LABEL(label84), buf0);
gtk_widget_destroy(window3);
window5 = create_window5 ();
  gtk_widget_show (window5);
}
  else {
sprintf(buf0,"Authentification erronne %s",buf0);
gtk_label_set_text(GTK_LABEL(label84), buf0);

}
}
if(choix==2)
{f1=fopen("code1.txt","r");
if (res_aut( login,password,f1)==1)
    {   
sprintf(buf0,"Authentification reussie %s",buf0);
gtk_label_set_text(GTK_LABEL(label84), buf0);
gtk_widget_destroy(window3);
window24 = create_window24 ();
  gtk_widget_show (window24);
}
  else {
sprintf(buf0,"Authentification erronne %s",buf0);
gtk_label_set_text(GTK_LABEL(label84), buf0);
}
}
}
void
on_button51_clicked     (GtkWidget *objet_graphique, gpointer user_data)
              
{GtkWidget *window4;
GtkWidget *window3;
window3=lookup_widget(objet_graphique, "window3");
gtk_widget_destroy(window4);
window3 = create_window3 ();
  gtk_widget_show (window3);

}


void
on_button50_clicked     (GtkWidget*objet_graphique, gpointer user_data)               
{char buf2[20] ="";
	GtkWidget *label104;
	GtkWidget *entry38;
	GtkWidget *entry39;
	

label104=lookup_widget (objet_graphique, "label104");
entry38=lookup_widget (objet_graphique, "entry38");
entry39=lookup_widget (objet_graphique, "entry39");
strcpy(login,gtk_entry_get_text(GTK_ENTRY(entry38)));
strcpy(password,gtk_entry_get_text(GTK_ENTRY(entry39)));
 
 if (choix==1)
   { f=fopen("code.txt","r");
     
     modif_id(f) ;       
        sprintf(buf2,"changement de coordonnee reussit %s",buf2);            }
gtk_label_set_text(GTK_LABEL(label104), buf2);
   if (choix==2)
   {   
                    modif_id(f1);
sprintf(buf2,"changement de coordonnee reussit %s",buf2);   
  gtk_label_set_text(GTK_LABEL(label104), buf2);
 } }   

void
on_button4_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window6;


window6 = create_window6();
gtk_widget_show(window6);

}


void
on_button5_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window11;


window11 = create_window11();
gtk_widget_show(window11);

}


void
on_button6_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void
on_button7_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window22;

window22 = create_window22();
gtk_widget_show(window22);

char chemin[]="evenements.log";

GtkWidget *scrolled_win, *textview1;
GtkTextBuffer *buffer;

gtk_container_set_border_width (GTK_CONTAINER (window22), 10);

textview1 = gtk_text_view_new ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview1));
gtk_text_buffer_set_text (buffer, "Your 1st GtkTextView widget!", -1);




}


void
on_button8_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button9_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button10_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window7;


window7= create_window7();
gtk_widget_show(window7);

}


void on_button11_clicked(GtkWidget *objet_graphique, gpointer user_data)   // Affichage ghalia !!
{
	GtkWidget *window8;
        
       	Patient p;
	FILE  *fg ;
        fg=fopen("patient.txt","r");
	;//char chemin[] = "patient.txt";
	char Buf0[1000] = "IDENTIFIANT: ";
	char Buf1[10] = "NOM: ";
	char Buf2[10] = "PRENOM: ";
	char Buf3[50] = "AGE:";
	char Buf4[6] = "ETAT:";
	GtkWidget * label10 ;
	GtkWidget * label5  ;
         
	label10= lookup_widget(objet_graphique, "label10");

	while (fscanf(fg,"%s %s %s %d %s",p.id,p.no,p.preno,&p.ag,p.eta)!=EOF)
           {
	    sprintf(Buf0, "%s %s \n\n %s %s \n\n %s %s \n\n %s %d \n\n  %s %s \n\n", Buf0, p.id, Buf1,p.no, Buf2, p.preno, Buf3, p.ag,      Buf4,p.eta);
            gtk_label_set_text(GTK_LABEL(label10), Buf0);
           }
        fclose(fg);
	window8 = create_window8();
	gtk_widget_show(window8);

            }


void on_button12_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window9;


window9 = create_window9();
gtk_widget_show(window9);

}


void
on_button13_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window10;


window10 = create_window10();
gtk_widget_show(window10);

}

void
on_button14_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button16_clicked(GtkWidget *objet_graphique, gpointer user_data)   // Ajouter ghalia
{
	GtkWidget *window8;
        Patient pp;
	FILE *fg ; 
        fg=fopen("patient.txt","w") ;
        GtkWidget *entry3;
	GtkWidget *entry4;
	GtkWidget *entry5;
	GtkWidget *entry6;
	GtkWidget *entry7;
	

	entry3 = lookup_widget(objet_graphique, "entry3");
	entry4 = lookup_widget(objet_graphique, "entry4");
	entry5 = lookup_widget(objet_graphique, "entry5");
	entry6 = lookup_widget(objet_graphique, "entry6");
	entry7 = lookup_widget(objet_graphique, "entry7");
	
      	 //copier la chaine saisie à partir de entry1 vers P.id
	strcpy(pp.id, gtk_entry_get_text(GTK_ENTRY(entry3)));
        //copier la chaine saisie à partir de entry1 vers P.nom
	strcpy(pp.no, gtk_entry_get_text(GTK_ENTRY(entry4)));
	//copier la chaine saisie à partir de entry2 vers P.prenom
	strcpy(pp.preno, gtk_entry_get_text(GTK_ENTRY(entry5)));
	//convertir la chaine saisie à partir de entry3 et la copier dans P.age
	pp.ag = atoi(gtk_entry_get_text(GTK_ENTRY(entry6)));
	 //copier la chaine saisie à partir de entry1 vers P.etat
	strcpy(pp.eta, gtk_entry_get_text(GTK_ENTRY(entry7)));
         fprintf (fg,"%s %s %s %d %s\n",pp.id,pp.no,pp.preno,pp.ag,pp.eta);       
          
	window8 = create_window8();
	gtk_widget_show(window8);
}

void on_button17_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window6;


window6 = create_window6();
gtk_widget_show(window6);
}
void
on_radiobutton1_group_changed          (GtkRadioButton  *radiobutton,
                                        gpointer         user_data)
{

}


void
on_radiobutton2_group_changed          (GtkRadioButton  *radiobutton,
                                        gpointer         user_data)
{

}


void
on_radiobutton3_group_changed          (GtkRadioButton  *radiobutton,
                                        gpointer         user_data)
{

}


void
on_radiobutton4_group_changed          (GtkRadioButton  *radiobutton,
                                        gpointer         user_data)
{

}


void
on_radiobutton5_group_changed          (GtkRadioButton  *radiobutton,
                                        gpointer         user_data)
{

}


void
on_button18_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button19_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button20_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button21_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button22_clicked(GtkWidget *objet_graphique, gpointer user_data)
{

GtkWidget *window12;


window12 = create_window12();
gtk_widget_show(window12);

}


void
on_button23_clicked(GtkWidget *objet_graphique, gpointer user_data) //   afficher ghada              
{
GtkWidget *window13;

	personnel m;
	char chemin[] = "personnel.txt";
	char Buf0[50] = "NOM: ";
	char Buf1[50] = "PRENOM: ";
	char Buf2[50] = "specialite: ";
	char Buf3[50] = "matricule: ";

	GtkWidget * label29;

	label29= lookup_widget(objet_graphique, "label29");

	afficher_personnel();

 sprintf(Buf0, " %s %d \n\n %s %s \n\n %s %s \n\n %s %s \n\n", Buf3, m.matricule,Buf0,m.nom, Buf1,m.prenom, Buf2, m.specialite);

	gtk_label_set_text(GTK_LABEL(label29), Buf0);
	
	window13 = create_window13();
	gtk_widget_show(window13);
}

void
on_button25_clicked(GtkWidget *objet_graphique, gpointer user_data)            
{

GtkWidget *window15;


window15 = create_window15 ();
gtk_widget_show(window15);

}


void
on_button26_clicked  (GtkWidget *objet_graphique, gpointer user_data)
{
	GtkWidget *window11 ;
	GtkWidget *window5;
	window11=lookup_widget(objet_graphique,"window11");
	window5 =create_window5 ();
	gtk_widget_show(window5);
	gtk_widget_destroy(window11);
	
}


void
on_button27_clicked(GtkWidget *objet_graphique, gpointer user_data)   // Ajouter ghada
{
	personnel medecin ;
	char chemin[] = "personnel.txt";
	GtkWidget *entry14;
	GtkWidget *entry15;
	GtkWidget *entry16;
	GtkWidget *entry17;
	GtkWidget *entry26;
	GtkWidget *window26;
	GtkWidget *window12;

	entry14 = lookup_widget(objet_graphique, "entry14");
	entry15 = lookup_widget(objet_graphique, "entry15");
	entry16 = lookup_widget(objet_graphique, "entry16");
	entry17 = lookup_widget(objet_graphique, "entry17");


	
	strcpy(medecin.nom, gtk_entry_get_text(GTK_ENTRY(entry15)));
	
	strcpy(medecin.prenom, gtk_entry_get_text(GTK_ENTRY(entry16)));
	
	medecin.matricule = atoi(gtk_entry_get_text(GTK_ENTRY(entry14)));

	strcpy(medecin.specialite, gtk_entry_get_text(GTK_ENTRY(entry17)));

	if (recherche_personnel_matricule(medecin.matricule,&medecin) == 0)
	ajouter_personnel(medecin.matricule,medecin);
	else 
	{
         window26 = create_window26();
         gtk_widget_show(window26);
	}
	window12 = create_window12();
	gtk_widget_show(window12);
        }
void on_button28_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
 	GtkWidget *window12 ;
	GtkWidget *window5;
	window12=lookup_widget(objet_graphique,"window12");
	window5 =create_window5 ();
	gtk_widget_show(window5);
	gtk_widget_destroy(window12);
}


void
on_button29_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button30_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button32_clicked(GtkWidget *objet_graphique, gpointer user_data)
{	personnel medecin;
	int ID;
	char chemin[] = "personnel.txt";
        
	GtkWidget *entry24;

	GtkWidget *window15;

	entry24 = lookup_widget(objet_graphique, "entry24");
	ID = atoi(gtk_entry_get_text(GTK_ENTRY(entry24)));


	supprimer_personnel(ID);
	window15 = create_window15();
	gtk_widget_show(window15);

}


void
on_button31_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
	GtkWidget *window15 ;
	GtkWidget *window5;
	window15=lookup_widget(objet_graphique,"window15");
	window5 =create_window5();
	gtk_widget_show(window5);
	gtk_widget_destroy(window15);
}


void on_button34_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window17;

window17 = create_window17();
gtk_widget_show(window17);
}


void
on_button35_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window18;

window18 = create_window18();
gtk_widget_show(window18);
}


void
on_button36_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window19;

window19 = create_window19();
gtk_widget_show(window19);

}


void
on_button37_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window20;

window20 = create_window20();
gtk_widget_show(window20);
}


void
on_button44_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window21;

window21 = create_window21();
gtk_widget_show(window21);
}


void
on_button33_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window4;

window4 = create_window4();
gtk_widget_show(window4);
}

void
on_button38_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button39_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void
on_button46_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void on_button45_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}





void
on_button40_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button41_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void
on_button43_clicked                    (GtkWidget *objet_graphique,    
                                        gpointer         user_data)     // Supprimer ghalia
{
   char x[7] ;
   FILE  *fg,*fg1 ;
   GtkWidget *entry35;
   Patient pp ;
   fg=fopen("patient.txt","r");
   fg1=fg=fopen("patient1.txt","w");
   entry35=lookup_widget (objet_graphique, "entry35");
   strcpy(x, gtk_entry_get_text(GTK_ENTRY(entry35)));
  	 while (fscanf(fg,"%s %s %s %d %s",pp.id,pp.no,pp.preno,&pp.ag,pp.eta)!=EOF)
  		{
			if (strcpy(x,pp.id)!=0)
                            fprintf(fg1,"%s %s %s %d %s",pp.id,pp.no,pp.preno,pp.ag,pp.eta) ;	
 		}  

   fclose(fg) ;
   fclose(fg1) ;
   remove ("patient.txt") ;
   rename("patient1.txt","patient.txt") ;
}


void
on_button42_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void
on_button48_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button47_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
GtkWidget *window16;

window16 = create_window16();
gtk_widget_show(window16);
}


void on_button64_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button65_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button66_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button67_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button69_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void on_button55_clicked(GtkWidget *objet_graphique, gpointer user_data)
{

	personnel medecin;
	int ID;
	char nom[10];
	char prenom[10];
	char specialite[10];
	char chemin[] = "personnel.txt";
	FILE *fichier_medecin;
	FILE *fichier_aide;
	fichier_medecin=fopen("personnel.txt","r");
	fichier_medecin=fopen("aide.txt","w");

	

	GtkWidget *entry42;
	GtkWidget *entry43;
	GtkWidget *entry44;
	GtkWidget *entry45;

	GtkWidget *window14;
	GtkWidget *window27;
	

	entry42 = lookup_widget(objet_graphique, "entry42");
	entry43 = lookup_widget(objet_graphique, "entry43");
	entry44 = lookup_widget(objet_graphique, "entry44");
	entry45 = lookup_widget(objet_graphique, "entry45");

	ID = atoi(gtk_entry_get_text(GTK_ENTRY(entry42)));
	
	strcpy(medecin.nom, gtk_entry_get_text(GTK_ENTRY(entry43)));
	
	strcpy(medecin.prenom, gtk_entry_get_text(GTK_ENTRY(entry44)));

	strcpy(medecin.specialite, gtk_entry_get_text(GTK_ENTRY(entry45)));
        
	if(recherche_personnel_matricule (ID,&medecin)== 0)
      	{ 
		window27 = create_window27();
		gtk_widget_show(window27); 
     	 }
	
	 if (nom1 == 1)
		strcpy(medecin.nom,"entry43");
	 else if (prenom1 == 1)
		strcpy(medecin.prenom,"entry44");
	 else
		strcpy(medecin.specialite,"entry45");


	while (fscanf(fichier_medecin,"%s %s %d %s",(medecin.nom),(medecin.prenom),&(medecin.matricule),(medecin.specialite))!=EOF) 
			{if(nom1==1) 

	                  	strcpy(medecin.nom,nom);
			 
            		else if ( prenom1==1)
                          
	                 	strcpy(medecin.prenom,prenom);
			  
	    	        else 

	                 	strcpy(medecin.specialite,specialite);}
	remove("personnel.txt");
	rename("aide.txt","personnel.txt");

     
	


	window14 = create_window14();
	gtk_widget_show(window14);

}


void on_button56_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
	GtkWidget *window14 ;
	GtkWidget *window5;
	window14=lookup_widget(objet_graphique,"window14");
	window5 =create_window5();
	gtk_widget_show(window5);
	gtk_widget_destroy(window14);
}


void on_button24_clicked(GtkWidget *objet_graphique, gpointer user_data)             
{

GtkWidget *window14;


window14 = create_window14();
gtk_widget_show(window14);
}




void
on_buttonK1_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button70_clicked     (GtkWidget*objet_graphique,gpointer user_data)       
{	GtkWidget *window3;
	GtkWidget *window2;
	choix=2;
	window3=lookup_widget(objet_graphique, "window3");
	gtk_widget_destroy(window2);
	window3 = create_window3 ();
  	gtk_widget_show (window3);
}

void
on_buttonK4_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_checkbutton2_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
nom1=1;
}


void
on_checkbutton3_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
prenom1=1;
}


void
on_checkbutton4_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
specialite1=1;
}


void
on_button71_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

